package com.java.servlet.web.commons;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BannedAccessFilter implements Filter {
	private ServletContext context;
	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
         
        String uri = req.getRequestURI();
        this.context.log("Requested Resource::"+uri);
         
//        HttpSession session = req.getSession(false);
         
        if(uri.endsWith("/JavaWebServlet/")){
            // pass the request along the filter chain
        	System.out.println("Rechazado    [  ");
            this.context.log("Unauthorized access request [(-_-)_.l.]");
            res.sendRedirect("/JavaWebServlet/");
        }else{
        	System.out.println("autoriza    [  ");
        	chain.doFilter(request, response);
        	this.context.log("Authorized access request [(-_-)_V]");
        }
		/*HttpServletRequest req = (HttpServletRequest) request;
        String requestURI = req.getRequestURI();
        System.out.println("doFilter  ");
        if (requestURI.startsWith("/JavaWebServlet/")) {
            String toReplace = requestURI.substring(requestURI.indexOf("/JavaWebServlet/"), requestURI.lastIndexOf("/") + 1);
            String newURI = requestURI.replace(toReplace, "");
            System.out.println("toReplace    [  " + toReplace);
            System.out.println("newURI    [  " + newURI);
            request.getRequestDispatcher(newURI).forward(request, response);
        } else {
        	 System.out.println("requestURI    [  " + requestURI);
            chain.doFilter(req, response);
        }
        */
		/*HttpServletRequest req = (HttpServletRequest)request;
		System.out.println("peticion    [  " + req.getRequestURL().toString());
		
		if (request instanceof HttpServletRequest) {
			 String url = ((HttpServletRequest)request).getRequestURL().toString();
			 String queryString = ((HttpServletRequest)request).getQueryString();
			 System.out.println("url    [   " + url + "  query   [   " + queryString);
			}
		
		 String uri = req.getRequestURI();
		 
		 StringBuffer urlBuffer = req.getRequestURL();
		 System.out.println("URL    [  " + uri);
		 
		 if (uri.equals("/JavaWebServlet/login/loginController") || uri.equals("/JavaWebServlet/login/loginController/")) {
		 urlBuffer.setLength(0);
		 urlBuffer.append("HOLA");
		 System.out.println("URL IGUAL    [  " + urlBuffer.toString());
		 } else {
			 urlBuffer.append("/JavaWebServlet/login/loginController");
			 System.out.println("URL DIFERENTE   [  " + urlBuffer.toString());
		 } 
		 
		 chain.doFilter(req,response);
        */
        /*
         *         final HttpServletRequestWrapper wrapped = new HttpServletRequestWrapper(request) {
            @Override
            public StringBuffer getRequestURL() {
                final StringBuffer originalUrl = ((HttpServletRequest) getRequest()).getRequestURL();
                return new StringBuffer("http://servername2:7001");
            }
        };
        chain.doFilter(wrapped, response);
         * */
		/*HttpServletRequest req = (HttpServletRequest) request;
		String requestingHost = req.getRemoteHost();
		String referringHost = getReferringHost(req.getHeader("Referer"));
		String bannedSite = null;
		boolean isBanned = false;
		if (bannedSiteTable.contains(requestingHost)) {
			bannedSite = requestingHost;
			isBanned = true;
		} else if (bannedSiteTable.contains(referringHost)) {
			bannedSite = referringHost;
			isBanned = true;
		}
		if (isBanned) {
			showWarning(response, bannedSite);// Custom response
		} else {
			chain.doFilter(request, response);
		}*/
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
        this.context = config.getServletContext();
        this.context.log("AuthenticationFilter initialized");
		/*bannedSiteTable = new HashSet<String>();
		String bannedSites = config.getInitParameter("bannedSites");
		StringTokenizer tok = new StringTokenizer(bannedSites);
		while (tok.hasMoreTokens()) {
			String bannedSite = tok.nextToken();
			bannedSiteTable.add(bannedSite);
			System.out.println("Baned [  " + bannedSite);
		}
*/
	}

}
